<?php


namespace app\admin\controller\Web;

use app\admin\model\WebCate;
use app\admincommon\controller\AdminController;
use think\facade\View;

class Category extends AdminController{

    protected $webcate;

    public function __construct()
    {
        $this->webcate = new WebCate();
         
    }


    function index(){

        $page = input("page") ? input("page")-1 : 0;
        $pagesize=10;

        $cate_list = $this->webcate->order("isshow desc,sort desc,id desc")->limit($page*$pagesize,$pagesize)->select();
        $total=$this->webcate->count();
        
        if(!empty($cate_list)){
            for($i=0;$i<count($cate_list);$i++){
                $cate_list[$i]["keywords_short"]=mb_substr($cate_list[$i]["keywords"], 0 , 20);
                $cate_list[$i]["descriptions_short"]=mb_substr($cate_list[$i]["descriptions"], 0 , 20);
            }
        }

        View::assign([
            "cate_list"=>$cate_list,
            "total"=>$total,
            "page"=>input("page")
        ]);

        return View::fetch();

    }

    //添加分类
    function add(){

        if(request()->isPost()){

            $post = request()->post();

            try {

                $save = $this->webcate->save($post);
            
            } catch (\Exception $e) {

                $this->error('保存失败：当前分类已存在');

            }

            $save ? $this->success('保存成功') : $this->error('保存失败');

        }

        return View::fetch();
        
    }

    //修改分类
    function edit(){

        $id=input("id");

        $cate = $this->webcate->find($id);

        empty($cate) && $this->error('分类不存在');

        if(request()->isPost()){

            $post = request()->post();

            try {

                $save = $this->webcate->update($post);
            
            } catch (\Exception $e) {

                $this->error('保存失败：当前分类已存在');

            }

            $save ? $this->success('保存成功') : $this->error('保存失败');

        }

        View::assign([
            "cate" => $cate
        ]);

        return View::fetch();

    }





}








?>